import * as THREE from '../../build/three.module.js';

const scene = new THREE.Scene();

const k = innerWidth / innerHeight,
      s = 70,
      camera = new THREE.PerspectiveCamera(s, k, 1, 1000);
camera.position.set(0, 0, 400);
camera.lookAt(scene.position);

const texture = new THREE.TextureLoader().load('../../textures/crate.gif');

const geometry = new THREE.BoxBufferGeometry(200, 200, 200);
const materiay = new THREE.MeshLambertMaterial({ map: texture });

const mesh = new THREE.Mesh(geometry, materiay);

const al = new THREE.AmbientLight(0xffffff);

scene.add(mesh);
scene.add(camera);
scene.add(al);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(devicePixelRatio);
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);

const render = () => {
  mesh.rotation.y += .005;
  mesh.rotation.x += .01;
  renderer.render(scene, camera);
}

const animation = () => {
  requestAnimationFrame(animation);
  render();
}
animation();

const handleWindowResize = () => {
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener('resize', handleWindowResize);